const authCodeFunc = require('../../../utils/authCode');
const Link = require('../../../models/blog_link');
const User = require('../../../models/admin_users');
const Validate = require('../../../utils/validate');
const knex = require('../../../models/knex')

/**
 * 友情链接控制器
 */
const ControllerLink = {
    /* 获取列表 */
    getListL: async function (req, res, next) {
        try {
            let { per_page, page, keywor } = req.query;
            let offset = (per_page * (page - 1)) || 0; //计算当前页
            let links = Link.table;

            const list = await knex(links)
                .limit(Number(per_page)).offset(Number(offset)) // 分页查询

            const total = (await Link.all()).length;
            let number = Math.ceil(total / per_page); // 计算出信息总页数

            res.json({ code: 200, data: { list, total, number } })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 获取详情 */
    getById: async function (req, res, next) {
        try {
            let { id } = req.query;
            const links = await Link.select({ id });
            res.json({ code: 200, msg: '成功连接', data: links[0] })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 创建 */
    Insert: async function (req, res, next) {
        try {
            let authorization = req.headers.authorization;
            let token = authorization.split(' ');
            let user_decode = authCodeFunc(token[1], 'DECODE');
            let user_info = user_decode.str.split('\t');
            let issuer_id = user_info[0];
            let { title, email, url, image_url, desc } = req.body;
            let params = { title, email, url, issuer_id, created_time: new Date().getTime() };

            if (!Validate.isEmail(email)) return res.json({ code: 0, msg: '请输入正确的邮箱!' });

            if (!Validate.isURL(url)) return res.json({ code: 0, msg: '请输入正确的域名链接!' });

            if (image_url) params.image_url = image_url;

            if (desc) params.desc = desc;

            await Link.insert(params);

            res.json({ code: 200, msg: '添加成功' })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 编辑 */
    update: async function (req, res, next) {
        try {
            let authorization = req.headers.authorization;
            let token = authorization.split(' ');
            let user_decode = authCodeFunc(token[1], 'DECODE');
            let user_info = user_decode.str.split('\t');
            let modifier_id = user_info[0];
            let { id, title, email, url, image_url, desc, display } = req.body;
            let params = {};

            if (display == 0 || display == 1) {
                await Link.update(id, { display });

                return res.json({ code: 200, msg: `${display ? '成功' : '取消'}显示` })
            } else {
                if (!Validate.isEmail(email)) return res.json({ code: 0, msg: '请输入正确的邮箱!' });

                if (!Validate.isURL(url)) return res.json({ code: 0, msg: '请输入正确的域名链接!' });

                params = { title, email, url, image_url, desc, modifier_id, update_time: new Date().getTime() };

                await Link.update(id, params)

                return res.json({ code: 200, msg: '修改成功' })
            }

        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 删除 */
    del: async function (req, res, next) {
        try {
            let { id } = req.body;
            await Link.delete(id);

            res.json({ code: 200, msg: '删除成功' })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    }
}

module.exports = ControllerLink;